package com.hhsj.test.redpoint;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.hhsj.redpoint.RedpointUtil;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.DrawableMarginSpan;
import android.text.style.DynamicDrawableSpan;
import android.text.style.IconMarginSpan;
import android.text.style.ImageSpan;
import android.text.style.ReplacementSpan;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    boolean show = false;
    boolean show2 = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RedpointUtil.getDefault().register(this);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        RedpointalbeLayout redpointalbeLayout = findViewById(R.id.redpoint_layout);
        redpointalbeLayout.addRedpointTags(new String[]{"22"});


        redpointalbeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!show) {
                    show = true;
                    RedpointUtil.getDefault().showRedpoint("22");
//                    RedpointUtil.showRedpoint("22", (ViewGroup) findViewById(R.id.content_layout));
                } else {
                    show = false;
                    RedpointUtil.getDefault().removeRedpoint("22");
//                    RedpointUtil.removeRedpoint("22", (ViewGroup) findViewById(R.id.content_layout));
                }
            }
        });

        RedpointalbeLayout2 redpointalbeLayout2 = findViewById(R.id.redpoint_layout2);
        redpointalbeLayout2.addRedpointTags(new String[]{"33"});


        redpointalbeLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!show2) {
                    show2 = true;
                    RedpointUtil.getDefault().showRedpoint("33");
//                    RedpointUtil.showRedpoint("22", (ViewGroup) findViewById(R.id.content_layout));
                } else {
                    show2 = false;
                    RedpointUtil.getDefault().removeRedpoint("33");
//                    RedpointUtil.removeRedpoint("22", (ViewGroup) findViewById(R.id.content_layout));
                }
            }
        });

        TextView test_textview = findViewById(R.id.test_textview);

        String string = test_textview.getText().toString();
//        test_textview.setText(Html.fromHtml(getMyString(string), new Html.ImageGetter() {
//            @Override
//            public Drawable getDrawable(String source) {
//                int fontH = 45;
//                int id = Integer.parseInt(source);
//                Drawable d = MainActivity.this.getResources().getDrawable(id);
//                int height = fontH;
//                int width = (int) ((float) d.getIntrinsicWidth() / (float) d.getIntrinsicHeight()) * fontH;
//                if (width == 0) {
//                    width = d.getIntrinsicWidth();
//                }
//                d.setBounds(0, 0, width, height);
//                return d;
//            }
//        }, null));

//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_house_sale_tag);
//        BitmapDrawable drawable1 = new BitmapDrawable(getResources(),bitmap);
//        SpannableStringBuilder span = new SpannableStringBuilder("  ");//由于文本和图片排列的时候插入第一个
//        int x27 = 28;
//        span.append(string);
        Drawable drawable = getResources().getDrawable(R.drawable.icon_house_sale_tag);
//        int drawableHeight = drawable.getIntrinsicHeight(); //28
//        int drawableWidth = drawable.getIntrinsicWidth();//56
//        int imagePadding = 0;
//        drawable.setBounds(new Rect(12, 0, drawableWidth + 12, drawableHeight));
//        ReplacementSpan image = new MyIm(drawable);
//        span.setSpan(image, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//        Drawable drawable1 = new BitmapDrawable();
//        drawable1.setBounds(new Rect(10, 10, 50, 10));
//        ReplacementSpan image2 = new MyIm(drawable1);
//        span.setSpan(image2, 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//
//        test_textview.setText(span);


        MyIm.setImageAndText(test_textview, drawable, string, 80, 40, 10, 20);





    }

    private String getMyString(String title) {
        return "<img src='" + R.drawable.icon_house_sale_tag
                + "'/>" + title;

    }

    @Override
    protected void onDestroy() {
        RedpointUtil.getDefault().unRegister(this);
        super.onDestroy();
    }


    public static class MyIm extends ImageSpan {
        MyIm(Drawable drawable) {
            super(drawable);
        }

//        @Override
//        public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
//            Drawable d = getDrawable();
//            Rect rect = d.getBounds();
//            if (fm != null) {
//                Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
//                // 获得文字、图片高度
//                int fontHeight = fmPaint.bottom - fmPaint.top;
//                int drHeight = rect.bottom - rect.top;
//                int top = drHeight / 2 - fontHeight / 4;
//                int bottom = drHeight / 2 + fontHeight / 4;
//                fm.ascent = -bottom;
//                fm.top = -bottom;
//                fm.bottom = top;
//                fm.descent = top;
//            }
//            return rect.right;
//        }

        //getSize方法，返回一个Int含义为图片的宽度还设置了文字的ascent、descent的位置
        @Override
        public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
            Drawable b = getDrawable();
            Paint.FontMetricsInt fm = paint.getFontMetricsInt();
            int transY = (y + fm.descent + y + fm.ascent) / 2 - b.getBounds().bottom / 2;
            canvas.save();
            canvas.translate(x, transY);
            b.draw(canvas);
            canvas.restore();
        }

        public static void setImageAndText(@NonNull TextView textView,
                                           @NonNull Drawable drawable,
                                           @NonNull String text,
                                           int targetDrawableWidth,
                                           int targetDrawableHeight,
                                           int leftMargin, int rightMargin) {
            SpannableStringBuilder span = new SpannableStringBuilder("  ");//由于文本和图片排列的时候插入第一个
            span.append(text);
            int drawableWidth = targetDrawableWidth <= 0 ? drawable.getIntrinsicWidth() : targetDrawableWidth;//56
            int drawableHeight = targetDrawableHeight <= 0 ? drawable.getIntrinsicHeight() : targetDrawableHeight; //28
            drawable.setBounds(new Rect(leftMargin < 0 ? 0 : leftMargin, 0, drawableWidth + leftMargin, drawableHeight));
            ReplacementSpan image = new MyIm(drawable);
            span.setSpan(image, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            Drawable drawable1 = new BitmapDrawable();
            drawable1.setBounds(new Rect(0, 0, rightMargin < 0 ? 0 : rightMargin, 0));
            ReplacementSpan image2 = new MyIm(drawable1);
            span.setSpan(image2, 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(span);
        }
    }
}
